package com.ljd.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljd.server.mapper.MailLogMapper;
import com.ljd.server.pojo.*;
import com.ljd.server.mapper.EmployeeMapper;
import com.ljd.server.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateConfigurer;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljd
 * @since 2022-01-25
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MailLogMapper mailLogMapper;



    /**
     * 分页查询用户列表
     * @param currentPage       当前页数
     * @param size              每页的条数
     * @param employee          条件
     * @param beginDateScope    日期条件
     * @return
     */
    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) {
        // 开启分页
        Page<Employee> page = new Page<>(currentPage,size);
        IPage<Employee> employeeByPage = employeeMapper.getEmployeeByPage(page, employee, beginDateScope);
        return new RespPageBean(employeeByPage.getTotal(), employeeByPage.getRecords());
    }

    /**
     * 获取最大工号+1
     * @return
     */
    @Override
    public RespBean getMaxWorkID() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        Integer maxWorkID = Integer.parseInt((String)maps.get(0).get("max(workID)")) + 1;
        // 通过String类型进行转换,长度不足8位用0补齐
        return RespBean.success(null,String.format("%08d",maxWorkID));
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public RespBean addEmp(Employee employee) {
        // 获取合同开始时间
        LocalDate beginContract = employee.getBeginContract();
        // 获取合同结束时间
        LocalDate endContract = employee.getEndContract();
        // 获取合同开始到结束距多少天
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));

        if(employeeMapper.insert(employee) == 1) {
            // 获取员工对象
            Employee emp = employeeMapper.getEmployee(employee.getId()).get(0);

            // 往数据库中添加将要发送的消息
            //String msgId = UUID.randomUUID().toString();
            //MailLog mailLog = new MailLog();
            //mailLog.setMsgId(msgId);
            //mailLog.setEid(employee.getId());
            //mailLog.setStatus(0);
            //mailLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
            //mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            //mailLog.setCount(MailConstants.MAX_TRY_COUNT);
            //mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            //mailLog.setCreateTime(LocalDateTime.now());
            //mailLog.setUpdateTime(LocalDateTime.now());
            //
            //mailLogMapper.insert(mailLog);




            // 发送信息
            rabbitTemplate.convertAndSend(
                    "mail-welcome",
                    emp);

            return RespBean.success("添加成功");
        }

        return RespBean.error("添加失败");
    }

    /**
     * 查询员工
     * @param id
     * @return
     */
    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }
}
